'use strict';

(function (app) {


    app.register.controller('schtotalCtrl', ['$scope', '$http', '$filter', '$rootScope', function ($scope, $http, $filter, $rootScope) {

        $rootScope.mask=true;

        $scope.draw = function (datas) {
            var recvflows = [];
            var sendflows = [];
            var curtimes = [];
            var onlines = [];
            var recvcurpackets = [];
            var sendcurpackets = [];
            for (var k in datas) {
                recvflows.push(datas[k].CurReceiveDataFlow)
                sendflows.push(datas[k].CurSendDataFlow)
                var date = new Date(datas[k].CurTime);
                curtimes.push((date.getYear() + 1900) + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds())
                onlines.push(datas[k].OnlineNum)
                recvcurpackets.push(datas[k].CurReceivePacketNum)
                sendcurpackets.push(datas[k].CurSendPacketNum)
            }

            var option = {
                title : {
                    text: '网速变化',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['实时接收', '实时发送']
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: curtimes
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '实时接收',
                        type: 'line',
                        stack: '字节/秒',
                        data: recvflows
                    },
                    {
                        name: '实时发送',
                        type: 'line',
                        stack: '字节/秒',
                        data: sendflows
                    }

                ]
            };


            var sessionoption = {
                title : {
                    text: '在线会话变化',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['会话数量']
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }

                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: curtimes
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '在线会话数量',
                        type: 'line',
                        stack: '个',
                        data: onlines
                    }
                ]
            };
            var ecoption_packets = {
                title : {
                    text: '报文数量',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'


                },
                legend: {
                    data: ['实时接收', '实时发送']
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: curtimes,
                        splitNumber: 10
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisLabel : {
                            formatter: '{value}包/秒'
                        }
                    }
                ],
                series: [
                    {
                        name: '实时接收报文数量',
                        type: 'line',
                        stack: '包/秒',
                        data: recvcurpackets,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    },
                    {
                        name: '实时发送报文数量',
                        type: 'line',
                        stack: '包/秒',
                        data: sendcurpackets,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }

                    }
                ]
            };
            $scope.ecoption_flow = option

            $scope.ecoption_session = sessionoption;
            $scope.ecoption_packets = ecoption_packets;
        }
        var count = 40;
        $scope.pageCount = count;

        $scope.startdate =moment().add(-1,"h").startOf("m");//moment.tz('UTC').add(-1, 'h').hour(12).startOf('h');// (start.getYear()+1900)+"-"+(start.getMonth+1)+"-"+(start.getDate()+1)+" "+start.getHours()+":"+start.getMinutes()+":"+start.getSeconds();
        $scope.enddate = moment();//moment.tz('UTC').hour(12).startOf('h');//(today.getYear()+1900)+"-"+(today.getMonth+1)+"-"+(today.getDate()+1)+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
        // $scope.sendfrom2zh=sendfrom2zh;
        $scope.pageChanged = function () {
            $rootScope.loading_text = "努力加载中..."
            $http.post("./data/schtotal", {
                    curPage: $scope.currentPage,
                    count: $scope.pageCount,
                    startdate: $scope.startdate.format("YYYY-MM-DD HH:mm:ss"),
                    enddate: $scope.enddate.format("YYYY-MM-DD HH:mm:ss"),

                })
                .success(function (response) {
                    $scope.data = response.Datas;
                    $scope.currentPage = response.CurPage
                    $scope.totalPage = response.TotalPage
                    $scope.draw(response.Datas)
                });
            // $log.log('Page changed to: ' + $scope.currentPage);

        };
        $scope.sch = function () {
            $rootScope.loading_text = "努力加载中...";
            console.log($scope.startdate)
            $http.post("./data/schtotal", {
                    curPage: 1,
                    count: $scope.pageCount,
                    startdate: $scope.startdate.format("YYYY-MM-DD HH:mm:ss"),
                    enddate: $scope.enddate.format("YYYY-MM-DD HH:mm:ss")


                })
                .success(function (response) {
                    $scope.data = response.Datas;
                    $scope.currentPage = response.CurPage
                    $scope.totalPage = response.TotalPage
                    $scope.draw(response.Datas)
                });
        };
        $scope.maxSize = 10;
        $scope.totalPage = 1;
        $scope.currentPage = 1;

    }])

})(app)